package com.jiuqi.lr_demo.utils;

import java.util.List;

public class Pagination {

    public static <T> List<T> paginateList(List<T> list, int pageNum, int pageSize) {
        //设置页码范围
        int fromIndex = (pageNum - 1) * pageSize;
        if (fromIndex >= list.size()) {
            return List.of();
        }
        int toIndex = Math.min(fromIndex + pageSize, list.size());
        return list.subList(fromIndex, toIndex);
    }

    public static Integer getPages(int size, Integer pageSize) {
        if (pageSize == null || pageSize <= 0) {
            return 1;
        }
        int pages = size / pageSize;
        if (size % pageSize > 0) {
            pages++;
        }
        return pages;
    }
}

